#coding:utf-8

from base.runmethod import RunMethod
from data.get_data import GetData
from util.common_util import CommonUtil
from data.dapendent_data import DependentData
from data.data_config import get_header_value
from util.send_email import SendEmail

class RunTest:

    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    # 程序执行主入口
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        rows_count = self.data.get_case_lines()
        # print rows_count-1
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                # 拿的请求数据
                data = self.data.get_request_data(i)
                print "请求数据："
                print data
                # 拿的json请求数据
                # request_data = self.data.get_data_for_json(i)
                # print request_data

                expect = self.data.get_expcet_data(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                if depend_case != None:
                    self.depend_data = DependentData(depend_case)
                    # 获取的依赖响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    # 获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    # request_data[depend_key] = depend_response_data
                # res = self.run_method.run_main(method, url, request_data, header)
                res = self.run_method.run_main(method, url, data, header)

                if self.com_util.is_contain(expect, res):
                    print "\n测试通过\n---\n返回数据:"
                    print res
                    print "\n\n<------------>\n"
                    self.data.write_result(i, 'PASS')
                    pass_count.append(i)
                else:
                    print "\n测试失败\n---\n"
                    print res
                    print "\n\n<------------>\n"
                    self.data.write_result(i, 'FAIL')
                    fail_count.append(i)

        print "!!!<------<------<------------>------>------>!!!"
        print "成功 " + str(len(pass_count)) + " 个case。"
        print "失败 " + str(len(fail_count)) + " 个case。"

        # self.send_mail.send_main(pass_count, fail_count)

if __name__ == '__main__':
    run = RunTest()
    # print run.go_on_run()
    # print get_header_value()
    run.go_on_run()
